package com.demo.react.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.demo.react.entity.Comment;
import com.demo.react.entity.Menu;
import com.demo.react.entity.Token;
import com.demo.react.service.CommentService;
import com.demo.react.service.TokenService;

@RestController
@RequestMapping("/react")
public class ReactController {

	private static Long count = 0l;

	@Autowired
	private CommentService commentService;

	@Autowired
	private TokenService tokenService;

	@RequestMapping("/comments")
	public Map<String, Object> commentList(Integer page) {

		count++;
		System.err.println("这是第 " + count + " 请求数据！");

		Sort sort = new Sort(Sort.Direction.DESC, "id");
		// pageno从0开始开始第一页
		Pageable pageable = new PageRequest((page - 1), 10, sort);
		Map<String, Object> map = new HashMap<>();
		try {
			Page<Comment> pageComment = commentService.commentList(pageable);
			System.out.println("Page:" + pageComment);
			map.put("commentList", pageComment);
			map.put("result", "success");
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "fail");
			return map;
		}
	}

	@RequestMapping("/submit")
	public Map<String, Object> submit(Comment comment,
			@RequestParam(value = "file", required = false) MultipartFile file,
			@RequestParam(value = "files", required = false) MultipartFile[] files) {
		Map<String, Object> map = new HashMap<>();
		try {
			comment.setDate(new Date());
			commentService.inserComment(comment);
			map.put("result", "success");
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "fail");
			return map;
		}
	}

	@RequestMapping("/login")
	public Map<String, Object> login(String name, String password) {
		System.out.println("name:" + name);
		System.out.println("password:" + password);
		Map<String, Object> map = new HashMap<>();
		map.put("result", "success");
		String tokenValue = UUID.randomUUID().toString();
		Date nextDate = dateAddHour(new Date(), 2);
		System.err.println(nextDate);
		System.err.println(new Date());
		Token token = new Token(name, tokenValue, new Date(), nextDate);
		tokenService.insertTaken(token);
		map.put("token", tokenValue);
		map.put("menu", getMenu());
		return map;

	}

	public Date dateAddHour(Date date, int hour) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.add(Calendar.HOUR_OF_DAY, hour);
		date = ca.getTime();
		String format = sdf.format(ca.getTime());
		try {
			date = sdf.parse(format);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;

	}

	public static void beforeOneHourToNowDate() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 2);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("一个小时前的时间：" + df.format(calendar.getTime()));
		System.out.println("当前的时间：" + df.format(new Date()));
	}

	public static List<Menu> getMenu() {
		List<Menu> list = new ArrayList<>();
		list.add(new Menu("/", "Home"));
		list.add(new Menu("/Login", "Login"));
		list.add(new Menu("/Home", "Home"));
		list.add(new Menu("/Message", "Message"));
		list.add(new Menu("/More", "More"));
		return list;
	}
}
